#!/usr/bin/env python
# coding=utf-8
from pycorenlp import StanfordCoreNLP
import json
import re
import sys


def Ssplit(text, outputFormat="json"):
    nlp = StanfordCoreNLP('http://10.141.209.137:9000')     
    output = nlp.annotate(text,
        properties={
            "annotators": "ssplit",  
            # 'ssplit.boundaryTokenRegex': '\\.|[!?]', # 分句的正则，默认为"\\.|[!?]+"，可能把".."标记为句末
            'outputFormat': outputFormat, 
        })
    if outputFormat == "json":
        if type(output) is unicode:
            output = re.sub(r"[\x00-\x09\x0B\x0C\x0E-\x1F]"," ", output)
            try:
                output = json.loads(output)
            except ValueError:
                import traceback
                traceback.print_exc()
                sys.stderr.write(text+"\n")
    return [' '.join(t['word'] for t in x['tokens']) for x in output['sentences']]

def Parse(text, outputFormat="json"):
    nlp = StanfordCoreNLP('http://10.141.209.139:9000')     
    output = nlp.annotate(text,
        properties={
            "annotators": "ner,regexner,parse",  
            'ner.useSUTime': 'false', # SUTime Tagger 默认启用，会自动识别数字，
            # 'ssplit.boundaryTokenRegex': '\\.|[!?]', # 分句的正则，默认为"\\.|[!?]+"，可能把".."标记为句末
            'regexner.mapping': '/home/winkar/nlp/regex_ner_map.txt',
            'outputFormat': outputFormat, 
        })
    # print output[:5360]
    if outputFormat == "json":
        if type(output) is unicode:
            output = re.sub(r"[\x00-\x09\x0B\x0C\x0E-\x1F]"," ", output)
            try:
                output = json.loads(output)
            except ValueError:
                import traceback
                traceback.print_exc()
                sys.stderr.write(text+"\n")
    return output

def NERTag(text):
    # nlp = StanfordCoreNLP('http://10.141.209.137:9000')   # 920b
    nlp = StanfordCoreNLP('http://10.141.209.139:9000')     # 920
    output = nlp.annotate(text,
        properties={
            "annotators": "tokenize,ssplit,ner,regexner",  
            'ner.useSUTime': 'false', # SUTime Tagger 默认启用，会自动识别数字，
            'ssplit.boundaryTokenRegex': '\\.|[!?]', # 分句的正则，默认为"\\.|[!?]+"，可能把".."标记为句末
            'regexner.ignorecase': "true",
            'regexner.mapping': '/home/winkar/nlp/regexner-map.txt',
            'outputFormat': 'json', 
        })
    # print output[:5360]
    if type(output) is unicode:
        output = re.sub(r"[\x00-\x09\x0B\x0C\x0E-\x1F]"," ", output)
        try:
            output = json.loads(output)
        except ValueError:
            import traceback
            traceback.print_exc()
            sys.stderr.write(text+"\n")
    return output

def main():
    pass
    

if __name__=="__main__":
    main()
